#include "iostream"
#include "stdlib.h"
 
#include "cv.h"
#include "highgui.h"

int main(){
     CvSize imgSize;
     imgSize.width = 640;
     imgSize.height = 480;
     IplImage* gr = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
     

     IplImage * resim;
     IplImage * mo;    
     int w,h;
     resim = cvLoadImage("lc.jpg",1);
     gr=cvLoadImage("mask.jpg",0);
     mo=cvLoadImage("mouth.jpg",0);
   CvRect bndRect = cvRect(0,0,0,0);
   CvRect mRect = cvRect(0,0,0,0);
   CvMemStorage* storage = cvCreateMemStorage(0);
   CvSeq* contour = 0;
   CvSeq* contour1 = 0;
   cvFindContours( gr, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
   cvFindContours( mo, storage, &contour1, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
   CvPoint pt1, pt2;
   CvPoint ma1, ma2;
      CvPoint ptm1, ptm2;
   CvPoint mam1, mam2;
   ma1.x=1;
   ma2.x=1;
   ma1.y=1;
   ma2.y=1;
      mam1.x=1;
   mam2.x=1;
   mam1.y=1;
   mam2.y=1;
    for( ; contour1 != 0; contour1 = contour1->h_next ){
   mRect = cvBoundingRect(contour1, 0);
   ptm1.x = mRect.x;
   ptm1.y = mRect.y;
   ptm2.x = mRect.x + mRect.width;
   ptm2.y = mRect.y + mRect.height;
   if(abs(ptm1.x-ptm2.x)>abs(mam1.x-mam2.x)){
   mam1.x=ptm1.x;
   mam2.x=ptm2.x;
   w=mRect.width;
    // if(abs(ptm1.y-ptm2.y)>abs(mam1.y-mam2.y)){
      mam1.y=ptm1.y;
      mam2.y=ptm2.y;
      h=mRect.height;
    // }
   }
//cvRectangle(resim, ptm1, ptm2, CV_RGB(0,255,0), 1);
}
 for( ; contour != 0; contour = contour->h_next ){
   bndRect = cvBoundingRect(contour, 0);
   pt1.x = bndRect.x;
   pt1.y = bndRect.y;
   pt2.x = bndRect.x + bndRect.width;
   pt2.y = bndRect.y + bndRect.height;
   
   if((((mam1.x>=(pt1.x-w/2) && mam1.x<=(pt2.x+w/2)) || (mam2.x<=(pt2.x+w/2) && mam2.x>=(pt1.x+w/2))) && (mam1.y>=(pt1.y-h/2) && mam2.y<=(pt2.y+h/2) )) && abs(pt1.x-pt2.x)>abs(ma1.x-ma2.x)){
   ma1.x=pt1.x;
   ma2.x=pt2.x;
      //   if((mam1.y>=(pt1.y-h/2) || mam2.y<=(pt2.y+h/2)) && abs(pt1.y-pt2.y)>abs(ma1.y-ma2.y)){
            ma1.y=pt1.y;
            ma2.y=pt2.y;
        // }
   }

cvRectangle(resim, pt1, pt2, CV_RGB(255,0,0), 1);
}   

   IplImage* crop = cvCreateImage(cvSize(abs(ma2.x-ma1.x),abs(ma2.y-ma1.y)), IPL_DEPTH_8U, 3);

   printf("Buyuk:\n%d %d\n%d %d\n\n",ma1.x,ma1.y,ma2.x,ma2.y);
   printf("Buyukm:\n%d %d\n%d %d\n\n",mam1.x,mam1.y,mam2.x,mam2.y);
   cvRectangle(resim, ma1, ma2, CV_RGB(0,255,0), 1);
   cvRectangle(resim, mam1, mam2, CV_RGB(0,0,255), 1);
   cvNamedWindow("My Window1", CV_WINDOW_AUTOSIZE);
   cvShowImage("My Window1",resim);
   mRect = cvRect(ma1.x,ma1.y,abs(ma2.x-ma1.x),abs(ma2.y-ma1.y));
   cvSetImageROI(resim,mRect);
   cvCopy(resim,crop,0);
   cvSaveImage("crop.jpg",crop);
   cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);
   cvShowImage("My Window",resim);
    cvWaitKey(20000);
    cvReleaseImage(&gr);
    cvReleaseImage(&crop);
    cvReleaseImage(&resim);
    cvDestroyWindow("My Window");
    cvDestroyWindow("My Window1");
    system("pause");
     return 0;
    }
